package com.ktjy.temp.mqtt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ktjy.temp.mqtt.mapper.DeviceDetailsMapper;
import com.ktjy.temp.mqtt.service.DeviceDetailsService;
import com.ktjy.temp.pojo.DeviceDetails;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeviceDetailsServiceImp implements DeviceDetailsService {

    @Resource
    DeviceDetailsMapper deviceDetailsMapper;

    @Override
    public int addDeviceData(DeviceDetails deviceDetails) {
        return deviceDetailsMapper.insert(deviceDetails);
    }

    @Override
    public List<DeviceDetails> getList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("connect_status", 1);
        queryWrapper.eq("status", 1);
        List<DeviceDetails> list = deviceDetailsMapper.selectList(queryWrapper);
        return list;
    }

    public int updataConnectStatus(DeviceDetails deviceDetails) {
        UpdateWrapper<DeviceDetails> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sensor_id", deviceDetails.getSensorId())
                .set("connect_status", deviceDetails.getConnectStatus())
                .set("update_time", deviceDetails.getUpdateTime());
        int count = deviceDetailsMapper.update(updateWrapper);
        return count;
    }
}
